# -*- coding: utf-8 -*-
# 适用于ap_lstm模型训练的数据处理模块
import pandas as pd
import datetime as dt
import apriori as ap
import os
from numpy import concatenate
import global_var as gl
import datetime


def get_year_mon_day(date):
    year = date // 10000
    mon = date % 10000 // 100
    day = date % 100
    year_str = str(year)
    mon_str = '0' + str(mon) if mon < 10 else str(mon)
    day_str = '0' + str(day) if day < 10 else str(day)
    return year_str + '-' + mon_str + '-' + day_str


def dateRange(beginDate, endDate):
    dates = []
    d = dt.datetime.strptime(beginDate, "%Y-%m-%d")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        d = d + dt.timedelta(1)
        date = d.strftime("%Y-%m-%d")
    # print(len(dates))
    return dates


def get_date_list(url=gl.org_url):
    ord_data = pd.read_csv(url, low_memory=False)
    # 确认日期类型为<class 'numpy.int64'>
    conf_date = list(ord_data['CONF_DATE'].unique())
    conf_date = sorted(conf_date)
    begin = conf_date[0]
    end = conf_date[-1]
    beginDate = get_year_mon_day(begin)
    endDate = get_year_mon_day(end)
    # for i in dateRange(beginDate, endDate):
    #     print(i)
    date_list = dateRange(beginDate, endDate)
    # print(date_list)
    return date_list


def date_to_int(date_list):
    new_date_list = []
    for date in date_list:
        new_date = int(date.replace('-', ''))
        new_date_list.append(new_date)
    return new_date_list


def get_steel_day(url=gl.org_url):
    ord_data = pd.read_csv(url, low_memory=False)
    # 读取确认日期集合
    conf_dates = ord_data['CONF_DATE'].unique()
    # 获取对应日期订单的钢种列表
    steel_day_list = []
    for conf_date in conf_dates:
        steel_day_list.append(list(ord_data['STLGRD'][ord_data['CONF_DATE'] == conf_date].unique()))
    return steel_day_list


def get_steel_list(url=gl.org_url):
    ord_data = pd.read_csv(url, low_memory=False)
    steel_list = ord_data['STLGRD'].unique()
    return steel_list


def get_daily_stlweight(url):
    ord_data = pd.read_csv(url, low_memory=False, encoding='gb2312')
    daily_stl_dict = dict()
    daily_stl_dict['CONF_DATE'] = date_to_int(get_date_list(url))
    date_num = len(get_date_list(url))
    stl_list = list(frozenset(get_steel_list()))
    print("总钢种个数为：", len(stl_list))
    for st in stl_list:
        # print(st)
        daily_stl_dict[st] = [0.0] * date_num
    # print('...')
    f = pd.DataFrame(daily_stl_dict)
    f.set_index('CONF_DATE', inplace=True)
    for index in ord_data.index:
        cd = ord_data.loc[index]['CONF_DATE']
        sg = ord_data.loc[index]['STLGRD']
        tw = ord_data.loc[index]['TOT_WGT']
        f.loc[cd][sg] += tw
        # print(tw)
    # f.to_csv('Data/day.csv')
    # 用时700s左右
    return f


def get_sorted_data(root_url):
    url_list = os.listdir(root_url)
    f_list = list()
    for url_name in url_list:
        f_list.append(get_daily_stlweight(os.path.join(root_url, url_name)))
    f = concatenate(f_list)
    print(f)


def get_relalted_steel(steel_type, url=gl.new_url):
    day_data = pd.read_csv(url, low_memory=False)
    # 待提取的钢种数据
    bilist = list(ap.getBiset(steel_type))  # 'A16050JJNON'
    b = [steel_type]
    for bl in bilist:
        # print(b)
        b.append(list(bl)[0])
    return len(b), b


# if __name__ == '__main__':
#     get_sorted_data('Data/2019order')
    # starttime = datetime.datetime.now()
    # print(starttime)
    # get_daily_stlweight()
    # print('...')
    # endtime = datetime.datetime.now()
    # print(endtime)
    # t = (endtime - starttime).seconds
    # print('预处理用时：' + str(t) + 's')
# 837s

